package cn.tedu.carehome.foundation.controller;

import cn.tedu.carehome.foundation.pojo.dto.EventAddNewDTO;
import cn.tedu.carehome.foundation.pojo.vo.*;
import cn.tedu.carehome.foundation.service.IBedService;
import cn.tedu.carehome.foundation.service.IEventService;
import cn.tedu.carehome.foundation.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/events")
@Api(tags = "5.事故管理模块")

public class    EventController {


    @Autowired
    private IEventService iEventService;

    public EventController() {
        log.debug("创建控制器类对象：EventController");
    }


    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('event/add-new')")
    @ApiOperation("添加事故")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(EventAddNewDTO eventAddNewDTO) {
        log.debug("开始处理【添加类别】的请求，参数：{}", eventAddNewDTO);
        iEventService.addNew(eventAddNewDTO);
        return JsonResult.ok();
    }


    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据id删除事故")
    @PreAuthorize("hasAuthority('event/delete')")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "事故ID", required = true, example = "9527", dataType = "long")
    public JsonResult<Void> delete(@Range(min = 1, max = 10000, message = "删除事故失败，事故ID非法！")
                                       @PathVariable Long id) {
        log.debug("开始【模拟】处理【根据id删除事故】的请求，参数：{}", id);
        iEventService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/pass")
    @ApiOperation("审核事故")
    @PreAuthorize("hasAuthority('event/update')")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id", value = "类别ID", required = true, example = "9527", dataType = "long")
    public JsonResult<Void> setPass(@Range(min = 1, max = 1000000, message = "审核事故失败，事故ID非法！")
                                      @PathVariable Long id,String checkInfo) {
        log.debug("开始处理【审核事故】的请求，参数：{}", id);
        iEventService.setPass(id,checkInfo);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/complete")
    @ApiOperation("完成事故")
    @PreAuthorize("hasAuthority('event/update')")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id", value = "类别ID", required = true, example = "9527", dataType = "long")
    public JsonResult<Void> setComplate(@Range(min = 1, max = 1000000, message = "完成事故失败，事故ID非法！")
                                    @PathVariable Long id,String checkInfo) {
        log.debug("开始处理【完成事故】的请求，参数：{}", id);
        iEventService.setComplate(id,checkInfo);
        return JsonResult.ok();
    }

    @GetMapping("/list")
    @ApiOperation(value="请求事故列表")
    @PreAuthorize("hasAuthority('event/read')")
    @ApiOperationSupport(order = 200)
    public  JsonResult<List<EventListItemVO>> select(){
        log.debug("开始处理后台请求事故列表");
        List<EventListItemVO> list=iEventService.list();
        return JsonResult.ok(list);
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation(value="根据id请求事故信息")
    @PreAuthorize("hasAuthority('event/update')")
    @ApiOperationSupport(order = 500)
    @ApiImplicitParam(name = "id", value = "事故ID", required = true, example = "9527", dataType = "long")
    public JsonResult<EventStandardVO> selectById(@Range(min = 1, max = 100000, message = "查询事故失败，事故ID非法！")
                                                 @PathVariable Long id){
        log.debug("开始处理[根据id查询事故的业务],参数:{}",id);
        EventStandardVO eventStandardVO=iEventService.getStandardById(id);
        return JsonResult.ok(eventStandardVO);
    }
}
